﻿using kis.api.model;
using kis.UC;
using System;
using System.Windows.Forms;

namespace kis
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Frm_UrlUpdate : BaseForm
    {
        /// <summary>
        /// 
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Frm_UrlUpdate(int id = 0)
        {
            InitializeComponent();

            this.ID = id;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void Form_KeyDown(object sender, KeyEventArgs e)
        {
            base.Form_KeyDown(sender, e);

            switch (e.KeyCode)
            {
                case Keys.F1:
                    DataUpdate();

                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void InitForm()
        {
            base.InitForm();

            this.uiTBox_id.ReadOnly = true;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void QueryData()
        {
            base.QueryData();

            if (this.ID > 0)
            {
                var item = api.KisUrlGetModel(this.ID);

                this.uiTBox_id.Text = item.id.ToString();
                this.uiUpDown_serial.Value = item.serial;
                this.uiTBox_Name.Text = item.name;
                this.uiTBox_JPM.Text = item.jpm;
                this.UITBox_Url.Text = item.url;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void DataUpdate()
        {
            Exec("保存数据", () =>
            {
                var item = new KisUrl();

                item.id = this.ID;
                item.serial = (int)this.uiUpDown_serial.Value;
                item.name = this.uiTBox_Name.Text.Trim();
                item.jpm = this.uiTBox_JPM.Text.Trim();
                item.url = this.UITBox_Url.Text.Trim();

                if (this.ID > 0)
                {
                    api.KisUrlUpdate(item);

                    ShowInfo("保存信息成功！");
                }
                else
                {
                    item = api.KisUrlAdd(item);

                    this.ID = item.id;
                    this.uiTBox_id.Text = item.id.ToString();

                    ShowInfo("添加数据成功！");
                }
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBtn_update_Click(object sender, EventArgs e)
        {
            DataUpdate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBtn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTBox_Name_TextChanged(object sender, EventArgs e)
        {
            var name = this.uiTBox_Name.Text.Trim();

            this.uiTBox_JPM.Text = GetJPM(name);
        }
    }
}
